﻿using System.Collections;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace LS.Screening.UI.Common
{
    /// <summary>
    /// 类型扩展方法
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetDisplay(this Type type)
        {
            IEnumerable<Attribute> customAttributes = type.GetCustomAttributes();
            foreach (Attribute item in customAttributes)
            {
                if (item is DisplayAttribute displayAttribute)
                {
                    return displayAttribute.Name ?? displayAttribute.Description;
                }
                if (item is DescriptionAttribute descriptionAttribute)
                {
                    return descriptionAttribute.Description;
                }
                if (item is DisplayNameAttribute displayNameAttribute)
                {
                    return displayNameAttribute.DisplayName;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static string GetDisplay(this MemberInfo member)
        {
            List<object> list = member.GetCustomAttributes(inherit: true).ToList();
            foreach (object item in list)
            {
                if (item is DisplayAttribute displayAttribute)
                {
                    return displayAttribute.Name ?? displayAttribute.Description;
                }
                if (item is DescriptionAttribute descriptionAttribute)
                {
                    return descriptionAttribute.Description;
                }
                if (item is DisplayNameAttribute displayNameAttribute)
                {
                    return displayNameAttribute.DisplayName;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ClassDescribe GetDescribe(this Type type)
        {
            return ClassDescribe.Get(type);
        }

        /// <summary>
        /// 判断是否为可空类型
        /// </summary>
        /// <param name="that"></param>
        /// <returns></returns>
        public static bool IsNullableType(this Type that)
        {
            return !(that == null) && !that.IsArray && that.FullName.StartsWith("System.Nullable`1[");
        }

        /// <summary>
        /// 判断是否为匿名类型
        /// </summary>
        /// <param name="that"></param>
        /// <returns></returns>
        public static bool IsAnonymousType(this Type that)
        {
            return !(that == null) && (that.FullName.StartsWith("<>f__AnonymousType") || that.FullName.StartsWith("VB$AnonymousType"));
        }

        /// <summary>
        /// 判断是否数组或者集合类型
        /// </summary>
        /// <param name="that"></param>
        /// <returns></returns>
        public static bool IsArrayOrList(this Type that)
        {
            return !(that == null) && (that.IsArray || typeof(IList).IsAssignableFrom(that));
        }
    }
}
